package com.xinchuang.mapper.targetSystem;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinchuang.entity.target.TargetBaseData;
import com.xinchuang.entity.targetSystem.system.SystemPosition;
import com.xinchuang.entity.targetSystem.system.TargetType;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName SystemPositionMapper
 * @Description: TODO(系统/体系位置信息表)
 * @Author zhangxuan
 * @Date 2022-05-06 15:04
 * @Verion V1.0
 */
public interface SystemPositionMapper extends BaseMapper<SystemPosition> {

    @Insert(value = {
            "INSERT INTO system_position (id,positions,viewDistances,height) ",
            "VALUES ",
            "(#{id},#{positions},#{viewDistances},#{height})",
            "ON DUPLICATE KEY UPDATE positions = #{positions}, viewDistances = #{viewDistances}, height = #{height}"
    })
    int addSystemPosition(SystemPosition position);

    @Select(value = {
            "SELECT positions,viewDistances,height FROM system_position WHERE id = #{id}"
    })
    SystemPosition getPositionById(@Param("id") String id);

    /**
     * 查询体系包含目标的类型统计
     * @param sysId
     * @return
     */
    @Select(value = {
            "SELECT tt.type_id,tt.type_name FROM",
            "(SELECT tb.type FROM target_base_data tb JOIN systems_relation sr ON tb.target_id = sr.target_id WHERE sr.sys_id = #{sysId} GROUP BY tb.type)",
            "AS a",
            "JOIN",
            "target_type tt",
            "ON",
            "a.type = tt.type_id"
    })
    @Results(
            value = {
                    @Result(property = "typeId", column = "type_id"),
                    @Result(property = "typeName", column = "type_name")
            }
    )
    List<TargetType> selectSystemsRelationTypeList(String sysId);

    @Select(value = {"SELECT tb.target_id,tb.target_name,tb.lon,tb.lat FROM target_base_data tb JOIN systems_relation sr ON tb.target_id = sr.target_id WHERE sr.sys_id = #{sysId} AND tb.type = #{typeId}"})
    @Results(
            value = {
                    @Result(property = "targetId", column = "target_id"),
                    @Result(property = "targetName", column = "target_name")
            }
    )
    List<TargetBaseData> selectTypeTargetList(String sysId, String typeId);

}
